﻿using Dapper;
using Estsh.Core.Base;
using Estsh.Core.Model.EnumUtil;
using Estsh.Core.Models;
using Estsh.Core.Services;
using Estsh.Core.Wms.IRepositories;
using Estsh.Core.Wms.IServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Estsh.Core.Wms.Services
{
    /// <summary>
    /// 委外退货
    /// </summary>
    public class OutSourceBackService : BaseService<BaseEntity>, IOutSourceBackService
    {
        private readonly IOutSourceBackRepository repository;
        public OutSourceBackService(IOutSourceBackRepository _repository) : base(_repository)
        {
            repository = _repository;
        }

        //获取所有委外退回单据信息
        public SetObjectDetail GetOutSourceBackOrderList(string orderNo)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();//返回消息
            List<WmsInstock> wmsMoves = repository.GetOutSourceBackOrderList(orderNo);
            if (wmsMoves.Count > 0)
            {
                rfInfo.type = "PASS";
                rfInfo.message = "成功";
                rfInfo.wmsObjectDetail = wmsMoves.ConvertAll(s => (object)s);
            }
            else
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "未查询到数据，请检查！";
            }
            return rfInfo;
        }

        //根据单据号获取委外退回信息
        public SetObjectDetail GetOutSourceBackOrderListByOrderNo(string orderNo)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();
            List<WmsInstock> wmsMoves = repository.GetOutSourceBackOrderListByOrderNo(orderNo);

            if (wmsMoves.Count > 0)
            {
                rfInfo.type = "PASS";
                rfInfo.message = "成功";
                rfInfo.wmsObjectDetail = wmsMoves.ConvertAll(s => (object)s);
            }
            else
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "未查询到数据，请检查！";
            }
            return rfInfo;
        }

        //根据库位获取库位信息
        public SetObjectDetail GetLocateByName(string locateName)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();
            List<SysLocate> sysLocates = repository.GetLocateByLocateName(locateName);

            SysStock sysStocksLocateCount = repository.GetStackQtySumByLocateName(locateName);//库位在库数

            if (sysLocates.Count == 0)
            {
                //提示  [A1-2-2]:该库位不存在，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + locateName + "]:该库位不存在，请检查!";
                return rfInfo;
            }
            else if (sysLocates[0].Enabled != "Y")
            {
                //提示  [A1-2-2]:该库位不可用，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + locateName + "]:该库位不可用，请检查!";
                return rfInfo;
            }
            else if (sysLocates[0].LocateType != (int)WmsEnumUtil.LocateType.NC_LOCATE)
            {
                //提示  [A1-2-2]:此库位码类型不是NC库位类型，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + locateName + "]:此库位码类型不是NC库位类型，请检查!";
                return rfInfo;
            }
            else if ((decimal)sysStocksLocateCount.Qty >= sysLocates[0].LocateCapacity)
            {
                //提示  [A1-2-2]:该库位容量已满，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + locateName + "]:该库位容量已满，请检查!";
                return rfInfo;
            }

            rfInfo.type = "PASS";
            rfInfo.message = "[" + locateName + "]:扫描成功,请扫描箱条码";
            return rfInfo;
        }

        public SetObjectDetail CheckStockByCartonAlocate(string cartonNo, string locateName, string orderNo, string loginId)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();
            List<SysStock> sysStocks = repository.GetStackInfoByCartonNo(cartonNo);

            if (sysStocks.Count == 0)
            {
                //提示  [M0090105]:该条码不可用，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:该条码不存在，请检查!";
                return rfInfo;
            }
            else if (sysStocks[0].Enabled != "Y")
            {
                //提示  [M0090105]:箱条码已冻结，无法操作!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:箱条码已冻结，无法操作!";
                return rfInfo;
            }
            else if (sysStocks[0].Status != (int)WmsEnumUtil.StockStatus.SHIPPED)
            {
                //提示  [M0090105]:该条码是**状态，无法操作!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:该条码是[" + sysStocks[0].StockStatus + "]状态，请检查!";
                return rfInfo;
            }

            List<SysLocate> sysLocates = repository.GetLocateByLocateNamePartNo(locateName);

            //根据库位名称获取目标信息
            List<SysLocate> locatesZone = repository.GetLocateZoneByLocateName(locateName);

            List<WmsInstock> WmsInstocks = repository.GetInstockByOrderNo(orderNo);
            if (WmsInstocks.Count == 0)
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + orderNo + "]:该单据是未创建或已关闭状态，请检查!";
                return rfInfo;
            }

            List<WmsInstockDetail> WmsInstockDetails = repository.GetInstockDetailByOrderNoPartNo(orderNo, sysStocks[0].PartNo);
            if (WmsInstockDetails.Count == 0)
            {
                //提示  [M0090105]:该条码是委外退回状态，无法操作!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:该条码零件号不在单据内!";
                return rfInfo;
            }


            //List<WmsInstockSn> moveSnCreates = repository.GetInstockSnByOrderNo(orderNo);
            //if (moveSnCreates.Count > 0)
            //{
            //    List<WmsInstockSn> WmsInstockSns = repository.GetInstockSnByOrderNoCartonNo(orderNo, cartonNo);
            //    if (WmsInstockSns.Count == 0)
            //    {
            //        rfInfo.type = "FAIL";
            //        rfInfo.message = "单据中不包含此包装条码，不允许此操作！";
            //        return rfInfo;
            //    }
            //}

            StringBuilder stringBuilder = new StringBuilder(1024);

            List<string> sqlStrings = new List<string>();
            List<DynamicParameters> parameterList = new List<DynamicParameters>();
            for (int i = 0; i < WmsInstockDetails.Count; i++)
            {

                int remainNum = Convert.ToInt32(WmsInstockDetails[i].Qty - WmsInstockDetails[i].RecQty);//余数
                if (sysStocks[0].Qty == remainNum)
                {
                    if (WmsInstocks[0].OrderStatus != (int)WmsEnumUtil.MoveOrderDetailStatus.BATCHING)//
                    {
                        stringBuilder = new StringBuilder(1024);
                        stringBuilder.Append("UPDATE wms_instock SET order_status=" + (int)WmsEnumUtil.MoveOrderDetailStatus.BATCHING + ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  order_no='" + orderNo + "';");
                        sqlStrings.Add(stringBuilder.ToString());
                    }

                    stringBuilder = new StringBuilder(1024);
                    stringBuilder.Append("UPDATE wms_instock_detail SET rec_qty+=" + sysStocks[0].Qty + ",item_status=" + (int)WmsEnumUtil.MoveOrderDetailStatus.COMPLETED + "" +
                        ",dest_warehouse_id=" + locatesZone[0].WarehouseId + ",dest_warehouse_name='" + locatesZone[0].WarehouseName + "',dest_zone_id=" + locatesZone[0].ZoneId + ",dest_zone_name='" + locatesZone[0].ZoneName + "',dest_erp_warehouse='" + locatesZone[0].ErpWarehouse + "'" +
                        " ,update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  ruid='" + WmsInstockDetails[i].Ruid + "';");
                    sqlStrings.Add(stringBuilder.ToString());

                    stringBuilder = new StringBuilder(1024);
                    stringBuilder.Append("UPDATE sys_stock SET status=" + (int)WmsEnumUtil.StockStatus.NC_INSTOCK + ",locate_id=" + sysLocates[0].LocateId + ",locate_name='" + locateName + "',zone_id=" + sysLocates[0].ZoneId + ",zone_name='" + sysLocates[0].ZoneName + "',warehouse_id=" + sysLocates[0].WarehouseId + ",warehouse_name='" + sysLocates[0].WarehouseName + "',update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + cartonNo + "';");
                    sqlStrings.Add(stringBuilder.ToString());

                    //判断条码是否在单据内，不在创建，在修改
                    List<WmsInstockSn> WmsInstockSns = repository.GetInstockSnByOrderNoCartonNo(orderNo, cartonNo);
                    if (WmsInstockSns.Count == 0)
                    {
                        stringBuilder = new StringBuilder(1024);
                        stringBuilder.Append("INSERT INTO dbo.wms_instock_sn(order_no,item_no,part_id,part_no,part_spec,carton_no ");
                        stringBuilder.Append("           ,qty,unit,status,factory_id,factory_code,enabled,create_userid,create_time) ");
                        stringBuilder.Append("     VALUES('" + orderNo + "',0," + sysStocks[0].PartId + ",'" + sysStocks[0].PartNo + "','" + sysStocks[0].PartSpec + "','" + sysStocks[0].CartonNo + "'," + sysStocks[0].Qty + " ");
                        stringBuilder.Append("           ,'" + sysStocks[0].Unit + "'," + (int)WmsEnumUtil.MoveOrderSnStatus.PROCESSED + "," + sysStocks[0].FactoryId + ",'" + sysStocks[0].FactoryId + "','Y'," + loginId + ",CONVERT(VARCHAR(20),GETDATE(),21) ) ");
                        sqlStrings.Add(stringBuilder.ToString());
                    }
                    else //修改
                    {
                        stringBuilder = new StringBuilder(1024);
                        //更新单据条码表
                        stringBuilder.Append("UPDATE dbo.wms_instock_sn ");
                        stringBuilder.Append("SET status = " + (int)WmsEnumUtil.MoveOrderSnStatus.PROCESSED + ", ");
                        stringBuilder.Append("    update_userid = " + loginId + ", ");
                        stringBuilder.Append("    update_time = CONVERT(VARCHAR(20),GETDATE(),21) ");
                        stringBuilder.Append("WHERE order_no = '" + orderNo + "' ");
                        stringBuilder.Append("      AND carton_no = '" + cartonNo + "'; ");
                        sqlStrings.Add(stringBuilder.ToString());
                    }

                }
                else if (sysStocks[0].Qty > remainNum)
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "[" + cartonNo + "]:该条码库存大于单据需求数量!";
                    return rfInfo;
                }
                else if (sysStocks[0].Qty < remainNum)
                {
                    //处理中
                    if (WmsInstocks[0].OrderStatus != (int)WmsEnumUtil.MoveOrderDetailStatus.BATCHING)//修改为盘点中状态
                    {
                        stringBuilder = new StringBuilder(1024);
                        stringBuilder.Append("UPDATE wms_instock SET order_status=" + (int)WmsEnumUtil.MoveOrderDetailStatus.BATCHING + ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  order_no='" + orderNo + "';");
                        sqlStrings.Add(stringBuilder.ToString());
                    }

                    stringBuilder = new StringBuilder(1024);
                    stringBuilder.Append("UPDATE wms_instock_detail SET rec_qty+=" + sysStocks[0].Qty + ",item_status=" + (int)WmsEnumUtil.MoveOrderDetailStatus.BATCHING + "" +
                        ",dest_warehouse_id=" + locatesZone[0].WarehouseId + ",dest_warehouse_name='" + locatesZone[0].WarehouseName + "',dest_zone_id=" + locatesZone[0].ZoneId + ",dest_zone_name='" + locatesZone[0].ZoneName + "',dest_erp_warehouse='" + locatesZone[0].ErpWarehouse + "'" +
                        ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  ruid='" + WmsInstockDetails[i].Ruid + "';");
                    sqlStrings.Add(stringBuilder.ToString());

                    stringBuilder = new StringBuilder(1024);
                    stringBuilder.Append("UPDATE sys_stock SET status=" + (int)WmsEnumUtil.StockStatus.NC_INSTOCK + ",locate_id=" + sysLocates[0].LocateId + ",locate_name='" + locateName + "',zone_id=" + sysLocates[0].ZoneId + ",zone_name='" + sysLocates[0].ZoneName + "',warehouse_id=" + sysLocates[0].WarehouseId + ",warehouse_name='" + sysLocates[0].WarehouseName + "',update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + cartonNo + "';");
                    sqlStrings.Add(stringBuilder.ToString());

                    stringBuilder = new StringBuilder(1024);
                    //更新单据条码表
                    stringBuilder.Append("UPDATE dbo.wms_instock_sn ");
                    stringBuilder.Append("SET status = " + (int)WmsEnumUtil.MoveOrderSnStatus.PROCESSED + ", ");
                    stringBuilder.Append("    update_userid = " + loginId + ", ");
                    stringBuilder.Append("    update_time = CONVERT(VARCHAR(20),GETDATE(),21) ");
                    stringBuilder.Append("WHERE order_no = '" + orderNo + "' ");
                    stringBuilder.Append("      AND carton_no = '" + cartonNo + "'; ");
                    sqlStrings.Add(stringBuilder.ToString());
                }
                stringBuilder = new StringBuilder(1024);
                stringBuilder.Append("INSERT INTO dbo.wms_subcontract_data ");
                stringBuilder.Append("           (trans_code ");
                stringBuilder.Append("           ,part_id ");
                stringBuilder.Append("           ,part_no ");
                stringBuilder.Append("           ,part_spec ");
                stringBuilder.Append("           ,carton_no ");
                stringBuilder.Append("           ,qty ");
                stringBuilder.Append("           ,deal_qty ");
                stringBuilder.Append("           ,unit ");
                stringBuilder.Append("           ,deal_status ");
                stringBuilder.Append("           ,vendor_id ");
                stringBuilder.Append("           ,vendor_code ");
                stringBuilder.Append("           ,ref_order_no ");
                stringBuilder.Append("           ,factory_id ");
                stringBuilder.Append("           ,factory_code ");
                stringBuilder.Append("           ,enabled ");
                stringBuilder.Append("           ,create_userid ");
                stringBuilder.Append("           ,create_time ");
                stringBuilder.Append("           ,guid) ");
                stringBuilder.Append("     VALUES ");
                stringBuilder.Append("           ('" + (int)WmsEnumUtil.TransType.OUTSOURCE_BACK + "' ");//委外退回
                stringBuilder.Append("    ," + sysStocks[0].PartId + " ");
                stringBuilder.Append("    ,N'" + sysStocks[0].PartNo + "'");
                stringBuilder.Append("    ,N'" + sysStocks[0].PartSpec + "'");
                stringBuilder.Append("    ,N'" + cartonNo + "'");
                stringBuilder.Append("    ," + sysStocks[0].Qty + "");
                stringBuilder.Append("          , 0 ");
                stringBuilder.Append("           ,'" + sysStocks[0].Unit + "' ");
                stringBuilder.Append("           ," + (int)WmsEnumUtil.WmsSubcontractDealStatus.CREATE + " ");//已创建
                stringBuilder.Append("           ," + sysStocks[0].VendorId + " ");
                stringBuilder.Append("           ,'" + sysStocks[0].VendorName + "' ");
                stringBuilder.Append("           ,'" + orderNo + "' ");
                stringBuilder.Append("           ," + sysStocks[0].FactoryId + " ");
                stringBuilder.Append("           ,'" + sysStocks[0].FactoryCode + "' ");
                stringBuilder.Append("           ,'Y' ");
                stringBuilder.Append("           ," + loginId + " ");
                stringBuilder.Append("           ,CONVERT(VARCHAR(20),GETDATE(),21) ");
                stringBuilder.Append("           ,NEWID()) ;");
                sqlStrings.Add(stringBuilder.ToString());

                stringBuilder = new StringBuilder(1024);
                stringBuilder.Append("INSERT INTO dbo.sys_stock_trans ");
                stringBuilder.Append("( ");
                stringBuilder.Append("    trans_code, ");
                stringBuilder.Append("    carton_no, ");
                stringBuilder.Append("    part_id, ");
                stringBuilder.Append("    part_no, ");
                stringBuilder.Append("    part_spec, ");
                stringBuilder.Append("    src_locate_id, ");
                stringBuilder.Append("    src_locate_name, ");
                stringBuilder.Append("    dest_locate_id, ");
                stringBuilder.Append("    dest_locate_name, ");
                stringBuilder.Append("    old_qty, ");
                stringBuilder.Append("    new_qty, ");
                stringBuilder.Append("    trans_qty, ");
                stringBuilder.Append("    old_status, ");
                stringBuilder.Append("    new_status, ");
                stringBuilder.Append("    unit, ");
                stringBuilder.Append("    factory_id, ");
                stringBuilder.Append("    factory_code, ");
                stringBuilder.Append("    src_erp_warehouse, ");
                stringBuilder.Append("    dest_erp_warehouse, ");
                stringBuilder.Append("    src_warehouse_id, ");
                stringBuilder.Append("    src_warehouse_name, ");
                stringBuilder.Append("    dest_warehouse_id, ");
                stringBuilder.Append("    dest_warehouse_name, ");
                stringBuilder.Append("    src_zone_id, ");
                stringBuilder.Append("    src_zone_name, ");
                stringBuilder.Append("    dest_zone_id, ");
                stringBuilder.Append("    dest_zone_name, ");
                stringBuilder.Append("    enabled, ");
                stringBuilder.Append("    create_userid, ");
                stringBuilder.Append("    create_time, ");
                stringBuilder.Append("    update_userid, ");
                stringBuilder.Append("    update_time, ");
                stringBuilder.Append("    guid ");
                stringBuilder.Append(") ");
                stringBuilder.Append("VALUES ");
                stringBuilder.Append(" ('" + (int)WmsEnumUtil.TransType.OUTSOURCE_BACK + "', ");//委外退回
                stringBuilder.Append("    '" + sysStocks[0].CartonNo + "', ");
                stringBuilder.Append("    " + sysStocks[0].PartId + ", ");
                stringBuilder.Append("    '" + sysStocks[0].PartNo + "',  ");
                stringBuilder.Append("    N'" + sysStocks[0].PartSpec + "', ");
                stringBuilder.Append("    " + sysStocks[0].LocateId + ",  ");//源库位
                stringBuilder.Append("    '" + sysStocks[0].LocateName + "',  ");
                stringBuilder.Append("    " + sysLocates[0].LocateId + ", ");//目标库位
                stringBuilder.Append("    '" + sysLocates[0].LocateName + "',  ");
                stringBuilder.Append("    " + sysStocks[0].Qty + ",");
                stringBuilder.Append("    " + sysStocks[0].Qty + ",");
                stringBuilder.Append("    " + sysStocks[0].Qty + ",");
                stringBuilder.Append("    '" + sysStocks[0].Status + "', ");
                stringBuilder.Append("    '" + (int)WmsEnumUtil.StockStatus.NC_INSTOCK + "', ");
                stringBuilder.Append("    '" + sysStocks[0].Unit + "', ");
                stringBuilder.Append("    " + sysStocks[0].FactoryId + ", ");
                stringBuilder.Append("    '" + sysStocks[0].FactoryCode + "',");
                stringBuilder.Append("    '" + sysStocks[0].ErpWarehouse + "',");
                stringBuilder.Append("    '" + sysStocks[0].ErpWarehouse + "', ");
                stringBuilder.Append("    " + sysStocks[0].WarehouseId + ", ");
                stringBuilder.Append("    '" + sysStocks[0].WarehouseName + "', ");
                stringBuilder.Append("    " + sysStocks[0].WarehouseId + ", ");
                stringBuilder.Append("    '" + sysStocks[0].WarehouseName + "', ");
                stringBuilder.Append("    " + sysStocks[0].ZoneId + ",  ");
                stringBuilder.Append("    '" + sysStocks[0].ZoneName + "',  ");
                stringBuilder.Append("    " + sysStocks[0].ZoneId + ", ");
                stringBuilder.Append("    '" + sysStocks[0].ZoneName + "',  ");
                stringBuilder.Append("    'Y', ");
                stringBuilder.Append("    " + loginId + ", ");
                stringBuilder.Append("    CONVERT(VARCHAR(20),GETDATE(),21), ");
                stringBuilder.Append("    " + loginId + ", ");
                stringBuilder.Append("    CONVERT(VARCHAR(20),GETDATE(),21), ");
                stringBuilder.Append("    NEWID()  ");
                stringBuilder.Append("    ) ; ");
                sqlStrings.Add(stringBuilder.ToString());
            }


            int refNum = repository.SQLTransaction(sqlStrings, parameterList);
            if (refNum > 0)
            {
                List<WmsInstock> wmsMoves = repository.GetInstockInfoByOrderNo(orderNo);

                if (wmsMoves.Count > 0)
                {
                    List<WmsInstockDetail> wmsMoves1 = repository.GetInstockDetailQtySumByOrderNo(orderNo);
                    for (int i = 0; i < wmsMoves1.Count; i++)
                    {
                        if (wmsMoves1[i].Qty != wmsMoves1[i].RecQty)
                        {
                            rfInfo.type = "PASS";
                            rfInfo.message = "操作成功!请继续扫描箱条码";
                            rfInfo.wmsObjectDetail = wmsMoves1.ConvertAll(s => (object)s);
                            return rfInfo;
                        }
                    }
                    int updateStatus = 1;
                    sqlStrings = new List<string>();
                    parameterList = new List<DynamicParameters>();
                    StringBuilder SqlStringBuilder = new StringBuilder(1024);
                    DynamicParameters parameters = new DynamicParameters();

                    List<WmsInstockDetail> wmsInstockDetails = repository.GetFinishInStockDetailByOrderNo(orderNo);
                    if (wmsInstockDetails.Count > 0)
                    {
                        SqlStringBuilder = new StringBuilder(1024);
                        SqlStringBuilder.Append("INSERT INTO dbo.WMS_MOVR(ZINSTNO,ZEILE,WERKS,LGORT,UMLGO,LIFNR,MATNR ");
                        SqlStringBuilder.Append("           ,ERFME,ACTION,ZPOST,ZTIME,SID,RECTIM,SYNFLG ");
                        SqlStringBuilder.Append("           ,GUID) ");
                        SqlStringBuilder.Append("     VALUES(@ZINSTNO,@ZEILE,@WERKS,@LGORT,@UMLGO,@LIFNR,@MATNR,@ERFME ");
                        SqlStringBuilder.Append("           ,@ACTION,@ZPOST,@ZTIME,@SID,@RECTIM,@SYNFLG ");
                        SqlStringBuilder.Append("           ,@GUID) ");

                        for (int i = 0; i < wmsInstockDetails.Count; i++)
                        {
                            SysPart sysPart = repository.GetPartNOInfoByPartNo(wmsInstockDetails[i].PartNo);
                            sqlStrings.Add(SqlStringBuilder.ToString());
                            parameters = new DynamicParameters();
                            parameters.Add("@ZINSTNO", wmsInstockDetails[i].OrderNo);//盘点凭证号
                            parameters.Add("@ZEILE", (i + 1) * 10);//行号
                            parameters.Add("@WERKS", wmsInstockDetails[i].FactoryCode);//工厂
                            parameters.Add("@LGORT", "");//发出库存地点
                            parameters.Add("@UMLGO", wmsInstockDetails[i].DestErpWarehouse);//接收库存地点
                            parameters.Add("@LIFNR", wmsInstockDetails[i].VendorCode);//供应商代码
                            parameters.Add("@MATNR", wmsInstockDetails[i].PartNo);//物料编码
                            parameters.Add("@ERFMG", wmsInstockDetails[i].RecQty);//转储数量
                            if (sysPart == null)
                            {
                                parameters.Add("@ERFME", "");//计量单位
                            }
                            else
                            {
                                parameters.Add("@ERFME", sysPart.Unit);//计量单位
                            }
                            parameters.Add("@ACTION", "M05"); //M05:委外退回
                            parameters.Add("@ZPOST", DateTime.Now.ToString("yyyyMMdd"));//交易日期
                            parameters.Add("@ZTIME", DateTime.Now.ToString("HHmmss"));//交易时间
                            parameters.Add("@SID", Guid.NewGuid().ToString().Replace("-", ""));
                            parameters.Add("@RECTIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                            parameters.Add("@SYNFLG", "N");
                            parameters.Add("@GUID", Guid.NewGuid().ToString());
                            parameterList.Add(parameters);
                        }

                        SqlStringBuilder = new StringBuilder(1024);
                        SqlStringBuilder.Append("UPDATE wms_instock SET order_status='40',update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  order_no=@orderNo;");
                        parameters = new DynamicParameters();
                        parameters.Add("@orderNo", orderNo);//委外退回单号
                        sqlStrings.Add(SqlStringBuilder.ToString());
                        parameterList.Add(parameters);

                        SqlStringBuilder = new StringBuilder(1024);
                        SqlStringBuilder.Append("UPDATE wms_instock_detail SET item_status='40',update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  order_no=@orderNo;");
                        parameters = new DynamicParameters();
                        parameters.Add("@orderNo", orderNo);//委外退回单号
                        sqlStrings.Add(SqlStringBuilder.ToString());
                        parameterList.Add(parameters);

                        updateStatus = repository.SQLTransaction(sqlStrings, parameterList);
                    }
                    if (updateStatus > 0)
                    {
                        rfInfo.type = "PASS";
                        rfInfo.message = "单据操作完成!";
                    }
                    else
                    {
                        rfInfo.type = "FAIL";
                        rfInfo.message = "单据操作操作失败!请检查";
                    }
                }
                else
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "未查询到数据，请检查！";
                }
            }
            else
            {
                //提示  操作失败!
                rfInfo.type = "FAIL";
                rfInfo.message = "未查询到数据，请检查！";
            }
            return rfInfo;
        }
        public SetObjectDetail SetOutSourceBackOrderNoSubmit(string orderNo, string loginId)
        {

            List<string> sqlStrings = new List<string>();
            List<DynamicParameters> parameterList = new List<DynamicParameters>();

            SetObjectDetail rfInfo = new SetObjectDetail();//返回消息
            StringBuilder SqlStringBuilder = new StringBuilder(1024);
            DynamicParameters parameters = new DynamicParameters();
            int updateStatus = 1;
            List<WmsInstockDetail> wmsInstockDetails = repository.GetFinishInStockDetailByOrderNo(orderNo);
            if (wmsInstockDetails.Count > 0)
            {
                SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append("INSERT INTO dbo.WMS_MOVR(ZINSTNO,ZEILE,WERKS,LGORT,UMLGO,LIFNR,MATNR,ERFMG ");
                SqlStringBuilder.Append("           ,ERFME,ACTION,ZPOST,ZTIME,SID,RECTIM,SYNFLG ");
                SqlStringBuilder.Append("           ,GUID) ");
                SqlStringBuilder.Append("     VALUES(@ZINSTNO,@ZEILE,@WERKS,@LGORT,@UMLGO,@LIFNR,@MATNR,@ERFME ");
                SqlStringBuilder.Append("           ,@ACTION,@ZPOST,@ZTIME,@SID,@RECTIM,@SYNFLG ");
                SqlStringBuilder.Append("           ,@GUID) ");

                for (int i = 0; i < wmsInstockDetails.Count; i++)
                {
                    SysPart sysPart = repository.GetPartNOInfoByPartNo(wmsInstockDetails[i].PartNo);
                    sqlStrings.Add(SqlStringBuilder.ToString());
                    parameters = new DynamicParameters();
                    parameters.Add("@ZINSTNO", wmsInstockDetails[i].OrderNo);//盘点凭证号
                    parameters.Add("@ZEILE", (i + 1) * 10);//行号
                    parameters.Add("@WERKS", wmsInstockDetails[i].FactoryCode);//工厂
                    parameters.Add("@LGORT", "");//发出库存地点
                    parameters.Add("@UMLGO", wmsInstockDetails[i].DestErpWarehouse);//接收库存地点
                    parameters.Add("@LIFNR", wmsInstockDetails[i].VendorCode);//供应商代码
                    parameters.Add("@MATNR", wmsInstockDetails[i].PartNo);//物料编码
                    parameters.Add("@ERFMG", wmsInstockDetails[i].RecQty);//转储数量
                    if (sysPart == null)
                    {
                        parameters.Add("@ERFME", "");//计量单位
                    }
                    else
                    {
                        parameters.Add("@ERFME", sysPart.Unit);//计量单位
                    }
                    parameters.Add("@ACTION", "M05"); //M05:委外退回
                    parameters.Add("@ZPOST", DateTime.Now.ToString("yyyyMMdd"));//交易日期
                    parameters.Add("@ZTIME", DateTime.Now.ToString("HHmmss"));//交易时间
                    parameters.Add("@SID", Guid.NewGuid().ToString().Replace("-", ""));
                    parameters.Add("@RECTIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                    parameters.Add("@SYNFLG", "N");
                    parameters.Add("@GUID", Guid.NewGuid().ToString());
                    parameterList.Add(parameters);
                }

                SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append("UPDATE wms_instock SET order_status='40',update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  order_no=@orderNo;");
                sqlStrings.Add(SqlStringBuilder.ToString());
                parameters = new DynamicParameters();
                parameters.Add("@orderNo", orderNo);
                parameterList.Add(parameters);

                SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append("UPDATE wms_instock_detail SET item_status='40',update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  order_no=@orderNo;");
                sqlStrings.Add(SqlStringBuilder.ToString());
                parameters = new DynamicParameters();
                parameters.Add("@orderNo", orderNo);
                parameterList.Add(parameters);

                updateStatus = repository.SQLTransaction(sqlStrings, parameterList);
            }

            if (updateStatus > 0)
            {
                rfInfo.type = "PASS";
                rfInfo.message = "[" + orderNo + "]单据已提交，请重新选择单据";
            }
            else
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "未查询到数据，请检查！";
            }
            return rfInfo;
        }
    }
}
